<?php

class MerchantForm extends BaseForm {
  const FORM_NAME           = 'frm';
  
  const FIELD_NAME          = 'name';
  const FIELD_ABN           = 'abn';
  //const FIELD_BANK_DETAILS  = 'bank_details';
  const FIELD_ACCOUNT_NAME  = 'account_name';
  const FIELD_BSB           = 'bsb';
  const FIELD_ACCOUNT_NUM   = 'account_number';
  const FIELD_EMAIL         = 'email';
  const FIELD_PHONE         = 'phone';
  const FIELD_MOBILE        = 'mobile';
  const FIELD_FAX           = 'fax';
  const FIELD_COMMISSION    = 'commision';
  const FIELD_BOOKING_FEE   = 'booking_fee';
  const FIELD_STATUS        = 'status';

  const SORT_NAME           = 'NAME';
  const SORT_CREATED_AT     = 'CREATED_AT desc';

  public function configure() {
    $arrStatusList = StringUtil::getStatusArray();

    $widget = array(
        self::FIELD_NAME          => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
        self::FIELD_ABN           => new sfWidgetFormInputText(array(), array('maxlength' => 30)),
        //self::FIELD_BANK_DETAILS  => new sfWidgetFormTextarea(array(), array()),
        self::FIELD_ACCOUNT_NAME  => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
        self::FIELD_BSB           => new sfWidgetFormInputText(array(), array('maxlength' => 100)),
        self::FIELD_ACCOUNT_NUM   => new sfWidgetFormInputText(array(), array('maxlength' => 50)),
        self::FIELD_EMAIL         => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
        self::FIELD_PHONE         => new sfWidgetFormInputText(array(), array('maxlength' => 30)),
        self::FIELD_MOBILE        => new sfWidgetFormInputText(array(), array('maxlength' => 30)),
        self::FIELD_FAX           => new sfWidgetFormInputText(array(), array('maxlength' => 30)),
        self::FIELD_COMMISSION    => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
        self::FIELD_BOOKING_FEE   => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
        self::FIELD_STATUS        => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true))
    );

    $labels = array(
        self::FIELD_NAME          => 'Name<req />',
        self::FIELD_ABN           => 'ABN<req />',
        //self::FIELD_BANK_DETAILS  => 'Bank Details',
        self::FIELD_ACCOUNT_NAME  => 'Account Name',
        self::FIELD_BSB           => 'BSB',
        self::FIELD_ACCOUNT_NUM   => 'Account No.',
        self::FIELD_EMAIL         => 'Email<req />',
        self::FIELD_PHONE         => 'Phone<req />',
        self::FIELD_MOBILE        => 'Mobile',
        self::FIELD_FAX           => 'Fax',
        self::FIELD_COMMISSION    => 'Commission<req />',
        self::FIELD_BOOKING_FEE   => 'Booking Fee<req />',
        self::FIELD_STATUS        => 'Status<req />'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
        self::FIELD_NAME          => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_NAME])))),
        self::FIELD_EMAIL         => new sfValidatorEmail(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_EMAIL])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_EMAIL])))),
        self::FIELD_PHONE         => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_PHONE])))),
        self::FIELD_COMMISSION    => new sfValidatorNumber(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_COMMISSION])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_COMMISSION])))),
        self::FIELD_BOOKING_FEE   => new sfValidatorNumber(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_BOOKING_FEE])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_BOOKING_FEE])))),
        self::FIELD_ABN           => new sfValidatorNumber(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_ABN])))),
        self::FIELD_STATUS        => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    return $values;
  }

  public function populateData(MERCHANT $merchant) {
    $this->setDefault(self::FIELD_NAME, $merchant->getName());
    $this->setDefault(self::FIELD_ABN, $merchant->getAbn());
    //$this->setDefault(self::FIELD_BANK_DETAILS, $merchant->getBankDetails());
    $this->setDefault(self::FIELD_ACCOUNT_NAME, $merchant->getAccountName());
    $this->setDefault(self::FIELD_BSB, $merchant->getBsb());
    $this->setDefault(self::FIELD_ACCOUNT_NUM, $merchant->getAccountNumber());
    $this->setDefault(self::FIELD_EMAIL, $merchant->getEmail());
    $this->setDefault(self::FIELD_PHONE, $merchant->getPhone());
    $this->setDefault(self::FIELD_MOBILE, $merchant->getMobile());
    $this->setDefault(self::FIELD_FAX, $merchant->getFax());
    $this->setDefault(self::FIELD_COMMISSION, $merchant->getCommission());
    $this->setDefault(self::FIELD_BOOKING_FEE, $merchant->getBookingFee());
    $this->setDefault(self::FIELD_STATUS, $merchant->getStatus());
  }
}
?>